﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
//【初阶篇】36.分支和循环do-while循环练习_上
//【初阶篇】37.分支和循环do-while循环练习_中
#include<stdio.h>
int main()
{
	//练习1 计算n的阶乘
	
	int i = 1;
	int n = 0;
	int ret = 1;
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		ret = ret * i;
	}
	printf("%d", ret);
	
	//-------------------

	//练习2.计算1!+2!+3!...+10!
	int i = 1;
	int n = 10;
	int j = 1;
	int ret = 1;
	int sum = 0;
	scanf("%d", &n);
	//for (i = 1; i <= n; i++)
	//{
	//	ret = 1;//这里要在每次阶乘的时候吧ret初始化
	//	for (j = 1; j <= i; j++)
	//	{
	//		
	//		ret = ret * j;
	//	}
	//	sum = sum + ret;
	//}

	//优化
	for (i = 1; i <= n; i++)
	{
		ret = ret * i;
		sum = sum + ret;
	}
	printf("%d", sum);
	//-------------------
	
	//练习3 在一个有序数组中查找具体的某个数字n
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	printf("输入1-10任意整数");
	scanf("%d", &k);
	///*	for (i = 0; i < sz; i++)
	//	{
	//		if (arr[i] == k)
	//		{
	//			printf("arr[%d]=%d",i, arr[i]);
	//			break;
	//		}
	//	}]=*/

	//	//二分法
	int l = 0;
	int r = sz - 1;
	
	while (l <= r)
	{
		int m = l+(r-l)/ 2; //这样写，在元素数量极大时，计算值不会超出上限
		if (arr[m] < k)
		{
			l = m + 1;
		}
		else if (arr[m] > k)
		{
			r = m - 1;
		}
		else
		{
			printf("arr[%d]=%d", m, arr[m]);
			break;
		}
	}
	if (l>r)
	{
		printf("未找到");

	}
	//-------------------
	

	//练习4 编写代码演示多个字符从两端移动向中间汇聚
	
	char st1[] = { "Wellcome to bit!!!" };
	char st2[] = { "##################" };
	int sl = 0;
	int sr = strlen(st1) - 1;
	while (sl <= sr)
	{
		st2[sl] = st1[sl];
		st2[sr] = st1[sr];
		sl++;
		sr--;
		printf("%s\n", st2);
	}
		printf("%d\n", sr);
	printf("%s\n", st1);
	
	//-------------------

	return 0;
}
